package feeelinganalyzer.analyzers;

import java.util.HashSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import feeelinganalyzer.crawler.CrawlerConstants;

public class StaticFeaturesExtractor {

	private String tweet;

	private boolean isReplyTweet;
	private boolean isRetweet;
	private boolean isMention;
	private boolean hasHashTag;
	private boolean hasLink;
	private int numberOfDifferentWords;

	public StaticFeaturesExtractor(String tweet) {
		this.tweet = tweet;
		process();
	}

	private void process() {
		numberOfDifferentWords = countDifferentWords(tweet.toLowerCase());
		checkIfIsReply();
		checkIfIsRT();
		checkIfIsMention();
		checkIfHasHashTag();
		checkIfHasLink();
	}

	private int countDifferentWords(String text) {
		HashSet<String> words = new HashSet<String>();
		int pos = 0;
		while (pos < text.length()) {
			String temp = "";
			while (pos < text.length() && Character.isLetter(text.charAt(pos))) {
				char c = text.charAt(pos);
				temp += c;
				pos++;
			}
			if (!temp.equals("")) {
				words.add(temp);
			}
			pos++;
		}
		return words.size();
	}

	private void checkIfIsMention() {
		String regularExp = ".+\\s+@\\w+\\s+.*";
		if (matches(regularExp, tweet) && !isReplyTweet && !isRetweet)
			isMention = true;
	}

	private void checkIfIsReply() {
		String regularExp = "@\\w+\\s+.*";
		if (matches(regularExp, tweet.toLowerCase()))
			isReplyTweet = true;
	}

	private void checkIfHasLink() {
		String regularExp = ".*http://\\w+\\.\\w+.+";
		if (matches(regularExp, tweet.toLowerCase()))
			hasLink = true;
	}

	private void checkIfHasHashTag() {
		String regularExp = ".*#.+";
		if (matches(regularExp, tweet.toLowerCase()))
			hasHashTag = true;
	}

	private void checkIfIsRT() {
		String regularExp = ".*\\s*" + TweetAnalyzerConstants.RT_INDICATORS
				+ "\\s+@\\w+.+";
		if (matches(regularExp, tweet.toLowerCase()))
			isRetweet = true;
	}

	private boolean matches(String regularExpression, String input) {
		Pattern p = Pattern.compile(regularExpression);
		Matcher matcher = p.matcher(input);
		return matcher.matches();
	}

	public boolean isReplyTweet() {
		return isReplyTweet;
	}

	public boolean isRetweet() {
		return isRetweet;
	}

	public boolean isMention() {
		return isMention;
	}

	public boolean hasHashTag() {
		return hasHashTag;
	}

	public boolean hasLink() {
		return hasLink;
	}

	public int getNumberOfDifferentWords() {
		return numberOfDifferentWords;
	}

	public String toString() {
		StringBuilder saida = new StringBuilder();
		saida.append("==== Analysing tweet: " + tweet + " ==== "
				+ CrawlerConstants.LINE_SEPARATOR);
		saida.append("==== Is RT? " + isRetweet() + " ==== "
				+ CrawlerConstants.LINE_SEPARATOR);
		saida.append("==== Is reply? " + isReplyTweet() + " ==== "
				+ CrawlerConstants.LINE_SEPARATOR);
		saida.append("==== Is metion? " + isMention() + " ==== "
				+ CrawlerConstants.LINE_SEPARATOR);
		saida.append("==== Has link? " + hasLink() + " ==== "
				+ CrawlerConstants.LINE_SEPARATOR);
		saida.append("==== Has hashTag? " + hasHashTag() + " ==== "
				+ CrawlerConstants.LINE_SEPARATOR);
		saida.append("==== Number of different words: "
				+ getNumberOfDifferentWords() + " ==== "
				+ CrawlerConstants.LINE_SEPARATOR);
		return saida.toString();
	}

	public void printReport() {
		System.out.println(toString() + CrawlerConstants.LINE_SEPARATOR);
	}
}
